<?php
// no direct access
defined( '_AppMiage' ) or die( 'Restricted access' );
if(isEtudiant()){
    //include header
    include_once (DS.'include/header.php');
    //include menu
    include_once (DS.'include/menu.php');

    $showOK = false;
    $showError = false;


    if(isset($_POST['submit']) && !empty($_POST['submit']))
    {
        $form = true;
        $errMSG = array();
        $check['offre'] = false;
        $check['entreprise'] = false;
        $check['user'] = false;
        $check['stage'] = false;
        $check['contact'] = false;

        // MAJ DES INFOS PERSOS
        if(isset($_POST['PERSO']['name']) && !empty($_POST['PERSO']['name']))
        {
            $tmp = $global_member->setNomUtilisateur($_POST['PERSO']['name']);
            if($tmp['boolOk'] === false)
            {
                $errMSG['PERSO']['nom'] = $tmp['errmsg'];
                $form = false;
            }
        }

        if(isset($_POST['PERSO']['prenom']) && !empty($_POST['PERSO']['prenom']))
        {
            $tmp = $global_member->setPrenomUtilisateur($_POST['PERSO']['prenom']);
            if($tmp['boolOk'] === false)
            {
                $errMSG['PERSO']['prenom'] = $tmp['errmsg'];
                $form = false;
            }
        }

        if(isset($_POST['PERSO']['mail']) && !empty($_POST['PERSO']['mail']))
        {
            $tmp = $global_member->setEmailUtilisateur($_POST['PERSO']['mail']);
            if($tmp['boolOk'] === false)
            {
                $errMSG['PERSO']['mail'] = $tmp['errmsg'];
                $form = false;
            }
        }

        if(isset($_POST['PERSO']['add1']) && !empty($_POST['PERSO']['add1']))
        {
            $tmp = $global_member->setAdresse1($_POST['PERSO']['add1']);
            if($tmp['boolOk'] === false)
            {
                $errMSG['PERSO']['add1'] = $tmp['errmsg'];
                $form = false;
            }
        }

        if(isset($_POST['PERSO']['add2']) && !empty($_POST['PERSO']['add2']))
        {
            $tmp = $global_member->setAdresse2($_POST['PERSO']['add2']);
            if($tmp['boolOk'] === false)
            {
                $errMSG['PERSO']['add2'] = $tmp['errmsg'];
                $form = false;
            }
        }

        if(isset($_POST['PERSO']['ville']) && !empty($_POST['PERSO']['ville']))
        {
            $tmp = $global_member->setVille($_POST['PERSO']['ville']);
            if($tmp['boolOk'] === false)
            {
                $errMSG['PERSO']['ville'] = $tmp['errmsg'];
                $form = false;
            }
        }


        if(isset($_POST['PERSO']['cp']) && !empty($_POST['PERSO']['cp']))
        {
            $tmp = $global_member->setCP($_POST['PERSO']['cp']);
            if($tmp['boolOk'] === false)
            {
                $errMSG['PERSO']['cp'] = $tmp['errmsg'];
                $form = false;
            }
        }
        // FIN MAJ INFOS PERSO


        // GESTION ENTREPRISE
        if(isset($_POST['ENT']['IdEntreprise']) && !empty($_POST['ENT']['IdEntreprise']))
        {
            // ENTREPRISE EXISTANT
            $IdEntreprise = $_POST['ENT']['IdEntreprise'];
            $check['entreprise'] = true;
        }
        else
        {
            // ENTREPRISE INEXISTANTE
            $entreprise = new Entreprise();
            if(isset($_POST['ENT']['Nom']) && !empty($_POST['ENT']['Nom']))
            {
                $tmp = $entreprise->setNomEntreprise($_POST['ENT']['Nom']);
                if($tmp['boolOk'] === false)
                {
                    $errMSG['ENT']['nom'] = $tmp['errmsg'];
                    $form = false;
                }
            }

            if(isset($_POST['ENT']['phone']) && !empty($_POST['ENT']['phone']))
            {
                $tmp = $entreprise->setTelephoneEntreprise($_POST['ENT']['phone']);
                if($tmp['boolOk'] === false)
                {
                    $errMSG['ENT']['phone'] = $tmp['errmsg'];
                    $form = false;
                }
            }

            if(isset($_POST['ENT']['url']) && !empty($_POST['ENT']['url']))
            {
                $tmp = $entreprise->setSiteEntreprise($_POST['ENT']['url']);
                if($tmp['boolOk'] === false)
                {
                    $errMSG['ENT']['url'] = $tmp['errmsg'];
                    $form = false;
                }
            }

            if(isset($_POST['ENT']['add1']) && !empty($_POST['ENT']['add1']))
            {
                $tmp = $entreprise->setAdresse1Entreprise($_POST['ENT']['add1']);
                if($tmp['boolOk'] === false)
                {
                    $errMSG['ENT']['add1'] = $tmp['errmsg'];
                    $form = false;
                }
            }

            if(isset($_POST['ENT']['add2']) && !empty($_POST['ENT']['add2']))
            {
                $tmp = $entreprise->setAdresse2Entreprise($_POST['ENT']['add2']);
                if($tmp['boolOk'] === false)
                {
                    $errMSG['ENT']['add2'] = $tmp['errmsg'];
                    $form = false;
                }
            }

            if(isset($_POST['ENT']['ville']) && !empty($_POST['ENT']['ville']))
            {
                $tmp = $entreprise->setVilleEntreprise($_POST['ENT']['ville']);
                if($tmp['boolOk'] === false)
                {
                    $errMSG['ENT']['ville'] = $tmp['errmsg'];
                    $form = false;
                }
            }

            if(isset($_POST['ENT']['cp']) && !empty($_POST['ENT']['cp']))
            {
                $tmp = $entreprise->setCPEntreprise($_POST['ENT']['cp']);
                if($tmp['boolOk'] === false)
                {
                    $errMSG['ENT']['cp'] = $tmp['errmsg'];
                    $form = false;
                }
            }
            $entreprise->insertEntreprise();
            if($entreprise->requeteOk === true)
            {
                $check['entreprise'] = true;
                $IdEntreprise = $entreprise->getIdEntreprise();
            }
        }
        // FIN GESTION ENTREPRISE

        if($form === true)
        {
            $global_member->update();
            if($global_member->requeteOk === true )
            {
                $check['user'] = true;
            }


            if($check['entreprise'] === true && !empty($IdEntreprise))
            {
                // INSERT TUTEUR
                if(isset($_POST['TUTEUR']['mail']) && !empty($_POST['TUTEUR']['mail']))
                {
                    $contact = ContactEntreprise::getUserByLogin($_POST['TUTEUR']['mail']);

                    if(!empty($contact))
                    {
                        // TUTEUR EXISTE DEJA
                        $IdTuteur = $contact->IdUtilisateur;
                        $check['contact'] = true;
                    }
                    else
                    {
                        // ON CREE LE TUTEUR EN TANT QUE CONTACT ENTREPRISE
                        // LINKE AVEC L'ENTREPRISE CREEE EN MEME TEMPS
                        $contact = new ContactEntreprise();
                         $form2= true;
                        if(isset($_POST['TUTEUR']['nom']) && !empty($_POST['TUTEUR']['nom']))
                        {
                            $tmp = $contact->setNomUtilisateur($_POST['TUTEUR']['nom']);
                            if($tmp['boolOk'] === false)
                            {
                                $errMSG['TUTEUR']['nom'] = $tmp['errmsg'];
                                $form2 = false;
                            }
                        }

                        if(isset($_POST['TUTEUR']['prenom']) && !empty($_POST['TUTEUR']['prenom']))
                        {
                            $tmp = $contact->setPrenomUtilisateur($_POST['TUTEUR']['prenom']);
                            if($tmp['boolOk'] === false)
                            {
                                $errMSG['TUTEUR']['prenom'] = $tmp['errmsg'];
                                $form2 = false;
                            }
                        }

                        if(isset($_POST['TUTEUR']['mail']) && !empty($_POST['TUTEUR']['mail']))
                        {
                            $tmp = $contact->setEmailUtilisateur($_POST['TUTEUR']['mail']);
                            if($tmp['boolOk'] === false)
                            {
                                $errMSG['TUTEUR']['mail'] = $tmp['errmsg'];
                                $form2 = false;
                            }
                        }

                        if(isset($_POST['TUTEUR']['fonction']) && !empty($_POST['TUTEUR']['fonction']))
                        {
                            $tmp = $contact->setFonction($_POST['TUTEUR']['fonction']);
                            if($tmp['boolOk'] === false)
                            {
                                $errMSG['TUTEUR']['fonction'] = $tmp['errmsg'];
                                $form2 = false;
                            }
                        }

                        if(isset($_POST['TUTEUR']['tel']) && !empty($_POST['TUTEUR']['tel']))
                        {
                            $tmp = $contact->setTelephoneContactEntreprise($_POST['TUTEUR']['tel']);
                            if($tmp['boolOk'] === false)
                            {
                                $errMSG['TUTEUR']['tel'] = $tmp['errmsg'];
                                $form2 = false;
                            }
                        }

                        $contact->setIdEntreprise((int)$IdEntreprise);
                        $contact->setIdTypeUser("4");
                        $contact->setDateCreation();
                        if($form2)
                        {
                            $contact->insert();

                            if($contact->requeteOk === true)
                            {
                                $IdTuteur = $contact->getIdUtilisateur();
                                $check['contact'] = true;
                            }
                        }

                    }
                }
            }

            if($check['user'] === true && $check['entreprise'] === true && $check['contact'] === true)
            {
                // ON A TOUTE LES INFOS ON PEUT SAVE LA FICHE
                $offre = new Offre();
                $offre->setTypeOffre("externe");
                $offre->setStatutOffre("en attente");
                $offre->setIdContactEntreprise($IdTuteur);
                $form3 = true;
                if(isset($_POST['IG']['NomStage']) && !empty($_POST['IG']['NomStage']))
                {
                    $tmp = $offre->setTitreOffre($_POST['IG']['NomStage']);
                    if($tmp['boolOk'] === false)
                    {
                        $errMSG['IG']['NomStage'] = $tmp['errmsg'];
                        $form3 = false;
                    }
                }

                if(isset($_POST['IG']['Description']) && !empty($_POST['IG']['Description']))
                {
                    $tmp = $offre->setDescriptionOffre($_POST['IG']['Description']);
                    if($tmp['boolOk'] === false)
                    {
                        $errMSG['IG']['Description'] = $tmp['errmsg'];
                        $form3 = false;
                    }
                }

                if(isset($_POST['IG']['Domaine']) && !empty($_POST['IG']['Domaine']))
                {
                    $tmp = $offre->setIdDomaineApplication($_POST['IG']['Domaine']);
                    if($tmp['boolOk'] === false)
                    {
                        $errMSG['IG']['Domaine'] = $tmp['errmsg'];
                        $form3 = false;
                    }
                }

                if(isset($_POST['IG']['techno']) && !empty($_POST['IG']['techno']))
                {
                    $tmp = $offre->setIdsTechnologie($_POST['IG']['techno']);
                    if($tmp['boolOk'] === false)
                    {
                        $errMSG['IG']['techno'] = $tmp['errmsg'];
                        $form3 = false;
                    }
                }
                if(isset($_POST['IG']['cat']) && !empty($_POST['IG']['cat']))
                {
                    $tmp = $offre->setIdsCategorie($_POST['IG']['cat']);
                    if($tmp['boolOk'] === false)
                    {
                        $errMSG['IG']['cat'] = $tmp['errmsg'];
                        $form3 = false;
                    }
                }

                if($form3)
                {
                    $offre->insertOffre();
                    if($offre->requeteOk === true)
                    {
                        $IdOffre = $offre->getIdOffre();
                        $check['offre'] = true;
                    }
                }
            }

            if($check['offre'] === true)
            {
                $form4 = true;
                $stage = new Stage();
                $stage->setIdEtudiant($global_member->getIdUtilisateur());
                $stage->setEtatFiche("en attente");
                $stage->setIdTuteurEntreprise($IdTuteur);
                $stage->setIdOffre($IdOffre);
                $stage->setIdPromo($global_member->getIdPromo());
                $stage->setDateCreationFicheStage();
                if(isset($_POST['IG']['NomStage']) && !empty($_POST['IG']['NomStage']))
                {
                    $tmp = $stage->setTitreStage($_POST['IG']['NomStage']);
                    if($tmp['boolOk'] === false)
                    {
                        $errMSG['IG']['NomStage'] = $tmp['errmsg'];
                        $form4 = false;
                    }
                }
                if(isset($_POST['IG']['Description']) && !empty($_POST['IG']['Description']))
                {
                    $tmp = $stage->setDescriptionStage($_POST['IG']['Description']);
                    if($tmp['boolOk'] === false)
                    {
                        $errMSG['IG']['Description'] = $tmp['errmsg'];
                        $form4 = false;
                    }
                }

                if(isset($_POST['IG']['dDebut']) && !empty($_POST['IG']['dDebut']))
                {
                    $tmp = $stage->setDateDebutStage(Tools::transformDateToTimestamp($_POST['IG']['dDebut']));
                    if($tmp['boolOk'] === false)
                    {
                        $errMSG['IG']['dDebut'] = $tmp['errmsg'];
                        $form4 = false;
                    }
                }
                if(isset($_POST['IG']['dFin']) && !empty($_POST['IG']['dFin']))
                {
                    $tmp = $stage->setDateFinStage(Tools::transformDateToTimestamp($_POST['IG']['dFin']));
                    if($tmp['boolOk'] === false)
                    {
                        $errMSG['IG']['dFin'] = $tmp['errmsg'];
                        $form4 = false;
                    }
                }
                if($form4)
                {
                    $stage->insertStage();
                    if($stage->requeteOk === true)
                    {
                        $IdStage = $stage->getIdStage();
                        $check['stage'] = true;
                        $showOK = true;
                        ?>
                        <script type="text/javascript">
                            window.location.href="<?php echo URL; ?>index.php?m=stage_etudiant&ok=1";
                        </script>
                        <?php
                    }
                }
            }
        }
        if($check['user'] === false || $check['entreprise'] === false
                || $check['contact'] === false || $check['offre'] === false
                || $check['stage'] === false)
        {
            $showError = true;
        }
    }

    $lstEntreprise = Entreprise::getList();
    $lstDomaine = DomaineApplication::getListDomaineApplication();
    $lstTechno = Technologie::getList();
    $lstCategorie = Categorie::getListCategory();
    ?>
    <div id="content">
        <div class="post">
            <div class="entry">
                <h1 class="title">Cr&eacute;ation d'une fiche de stage</h1>
                &nbsp;
                <?php if($showError){ ?>
                <div class="error">
                    Un probl&egrave;me est survenue veuillez verifier vos informations.
                    <br />
                    <?php
                    vardump($offre);
                    ?>
                </div>
                <?php } ?>
            </div>
            <form method="post" action="<?php echo URL; ?>index.php?m=submit_offre_externe" id="custom">
                <fieldset title="&Eacute;tape 1">
                    <legend>Informations G&eacute;n&eacute;rales</legend>
                    <label for="IG[NomStage]">Titre du stage : </label><br />
                    <input type="text" class="text" name="IG[NomStage]" value="<?php echo $_POST['IG']['NomStage']; ?>"/>
                    <br />
                    <label for="IG[dDebut]">Date de debut : </label><br />
                    <input type="text" class="text datepicker" name="IG[dDebut]" value="<?php echo $_POST['IG']['dDebut']; ?>"/>
                    <br />
                    <label for="IG[dFin]">Date de fin : </label><br />
                    <input type="text" class="text datepicker" name="IG[dFin]" value="<?php echo $_POST['IG']['dFin']; ?>"/>
                    <br />
                    <label for="IG[Description]">Description : </label>
                    <span class="miniInfos">*Le sujet doit &ecirc;tre en ad&eacute;quation avec le niveau du dipl&ocirc;me MIAGE pr&eacute;par&eacute;</span><br />
                    <textarea name="IG[Description]" class="text"><?php echo $_POST['IG']['Description']; ?></textarea>
                    <br />
                    <label for="IG[techno]">Technologie utilis&eacute;es : </label><br />
                    <table cellspacing="0">
                    <?php
                    $i = 0;
                    foreach($lstTechno as $k=>$e)
                    {
                        if($i == 0)
                        {
                            echo "<tr>";
                        }
                        ?>

                            <td style="border-left:1px #999 solid;">
                                <label for="IG[techno-<?php echo $e->idTechnologie; ?>]" style="font-weight:normal;cursor:pointer;">
                                    <?php echo $e->NomTechnologie; ?>
                                </label>
                            </td>
                            <td style="border-right:1px #999 solid;"><input type="checkbox" name="IG[techno][]" value="<?php echo $e->idTechnologie; ?>" /></td>
                        <?php
                        if($i == 3)
                        {
                            echo "</tr>";
                            $i=0;
                        }
                        else
                        {
                            $i++;
                        }
                    }
                    ?>
                    </table>
                    <br />
                    <label for="Categorie">Cat&eacute;gorie :</label><br />
                    <table>
                    <?php
                    $i = 0;
                    foreach($lstCategorie as $k=>$e)
                    {
                        if($i == 0)
                        {
                            echo "<tr>";
                        }
                        ?>

                            <td>
                                <label for="IG[cat-<?php echo $e->IdCategorie; ?>]" style="font-weight:normal;cursor:pointer;">
                                <?php echo $e->NomCategorie; ?>
                                </label>
                            </td>
                            <td><input type="checkbox" name="IG[cat][]" value="<?php echo $e->IdCategorie; ?>" /></td>
                        <?php
                        if($i == 1)
                        {
                            echo "</tr>";
                            $i=0;
                        }
                        else
                        {
                            $i++;
                        }
                    }
                    ?>
                    </table>
                    <br />
                    <label for="IG[Domaine]">Domaine d'applications du stage :</label><br />
                    <select name="IG[Domaine]">
                        <option>-- S&eacute;lectionner un domaine --</option>
                        <?php
                        foreach($lstDomaine as $i=>$e)
                        {
                            ?><option value="<?php echo $e->idDomaineApplication; ?>"><?php echo utf8_encode($e->NomDomaineApplication); ?></option><?php
                        }
                        ?>
                    </select>
                    <br />
                </fieldset>
                <fieldset title="&Eacute;tape 2">
                    <legend>Entreprise</legend>
                    <div id="searchEntreprise">
                       <label for="preEntreprise">S&eacute;lectionner une entreprise :<br />
                       <span style="font-size:10px;font-weight: normal;">* Pour cr&eacute;er une nouvelle entreprise, veuillez remplir les champs ci-dessous.</span><br />
                       </label>
                        <select name="ENT[preEntreprise]" id="searchPreEntreprise" onchange="ajax.getEntrepriseById(this.value);">
                            <option value="0">-- S&eacute;lectionner une entreprise --</option>
                            <?php
                            foreach($lstEntreprise as $i=>$e)
                            {
                                ?><option value="<?php echo $e->IdEntreprise; ?>"><?php echo $e->NomEntreprise; ?></option><?php
                            }
                            ?>
                        </select>
                        <br />
                        <br />
                        <hr />
                    </div>
                    <div id="searchHelp" style="display:none;"><a onclick="clearNewEntreprise();">Annuler la s&eacute;lection et cr&eacute;er une entreprise</a><br /><br /><hr /></div>
                    <div id="OldEntreprise" style="display:none;">
                        <input type="hidden" name="ENT[IdEntreprise]" value="0" id="IdEntreprise" />
                        <label for="NomEntreprise">Nom : </label>
                        <span id="nom"></span>
                        <br />
                        <label for="add1Entreprise">Adresse : </label>
                        <span id="add1"></span>
                        <br />
                        <label for="add2Entreprise">Adresse compl&eacute;mentaire : </label>
                        <span id="add2"></span>
                        <br />
                        <label for="cpEntreprise">Code Postal : </label>
                        <span id="cp"></span>
                        <br />
                        <label for="villeEntreprise">Ville : </label>
                        <span id="ville"></span>
                        <br />
                        <label for="paysEntreprise">Pays : </label>
                        <span id="pays"></span>
                        <br />
                        <label for="phoneEntreprise">T&eacute;l&eacute;phone : </label>
                        <span id="tel"></span>
                        <br />
                        <label for="urlEntreprise">Site web : </label>
                        <span id="url"></span>
                        <br />
                    </div>
                    <div id="NewEntreprise">
                        <label for="ENT[Nom]">Nom : </label><br />
                        <input type="text" class="text" name="ENT[Nom]" id="nom"/>
                        <br />
                        <label for="ENT[add1]">Adresse : </label><br />
                        <input type="text" class="text" name="ENT[add1]" id="add1"/>
                        <br />
                        <label for="ENT[add2]">Adresse compl&eacute;mentaire : </label><br />
                        <input type="text" class="text" name="ENT[add2]" id="add2"/>
                        <br />
                        <label for="ENT[cp]">Code Postal : </label><br />
                        <input type="text" class="text" name="ENT[cp]" id="cp"/>
                        <br />
                        <label for="ENT[ville]">Ville : </label><br />
                        <input type="text" class="text" name="ENT[ville]" id="ville"/>
                        <br />
                        <label for="ENT[pays]">Pays : </label><br />
                        <input type="text" class="text" name="ENT[pays]" id="pays"/>
                        <br />
                        <label for="ENT[phone]">T&eacute;l&eacute;phone : </label><br />
                        <input type="text" class="text" name="ENT[phone]" id="tel"/>
                        <br />
                        <label for="ENT[url]">Site web : </label><br />
                        <input type="text" class="text" name="ENT[url]" id="url"/>
                        <br />
                    </div>
                </fieldset>
                <fieldset title="&Eacute;tape 3">
                    <legend>Tuteur</legend>
                    <label for="TUTEUR[nom]">Nom : </label><br />
                    <input type="text" class="text " name="TUTEUR[nom]"/>
                    <br />
                    <label for="TUTEUR[prenom]">Pr&eacute;nom : </label><br />
                    <input type="text" class="text " name="TUTEUR[prenom]"/>
                    <br />
                    <label for="TUTEUR[fonction]">Fonction : </label><br />
                    <input type="text" class="text " name="TUTEUR[fonction]"/>
                    <br />
                    <label for="TUTEUR[tel]">T&eacute;l&eacute;phone : </label><br />
                    <input type="text" class="text " name="TUTEUR[tel]"/>
                    <br />
                    <label for="TUTEUR[mail]">Email : </label><br />
                    <input type="text" class="text  email" name="TUTEUR[mail]"/>
                    <br />
                </fieldset>
                <fieldset title="&Eacute;tape 4">
                    <legend>Mes Informations</legend>
                    <label for="PERSO[name]">Nom : </label><br />
                    <input class="text " type="text" name="PERSO[name]" id="name" value="<?php echo $global_member->getNomUtilisateur(); ?>"/><br />
                    <br />
                    <label for="PERSO[prenom]">Pr&eacute;nom : </label><br />
                    <input class="text " type="text" name="PERSO[prenom]" id="prenom" value="<?php echo $global_member->getPrenomUtilisateur(); ?>"/><br />
                    <br />
                    <label for="PERSO[mail]">Email : </label><br />
                    <input class="text  email" type="text" name="PERSO[mail]" id="mail" value="<?php echo $global_member->getEmailUtilisateur(); ?>"/><br />
                    <br />
                    <label for="PERSO[add1]">Adresse : </label><br />
                    <input class="text " type="text" name="PERSO[add1]" id="add1" value="<?php echo $global_member->getAdresse1(); ?>"/><br />
                    <br />
                    <label for="PERSO[add2]">Adresse compl&eacute;mentaire : </label><br />
                    <input class="text" type="text" name="PERSO[add2]" id="add2" value="<?php echo $global_member->getAdresse2(); ?>"/><br />
                    <br />
                    <label for="PERSO[cp]">Code Postal : </label><br />
                    <input class="text " type="text" name="PERSO[cp]" id="cp" value="<?php echo $global_member->getCP(); ?>"/><br />
                    <br />
                    <label for="PERSO[ville]">Ville : </label><br />
                    <input class="text " type="text" name="PERSO[ville]" id="ville" value="<?php echo $global_member->getVille(); ?>"/><br />
                    <br />
                    <!--label for="PERSO[tel]">T&eacute;l&eacute;phone : </label><br />
                    <input type="text" class="text" name="PERSO[tel]"/>
                    <br /-->
                </fieldset>
                <input type="submit" class="finish" disable="disable" value="Cr&eacute;er" name="submit">
            </form>
        </div>
    </div>
    <?php
    include_once (DS.'include/sidebar.php');
    include_once (DS.'include/footer.php');
}
else
{
    redirectTo();
}
?>